package com.example.basefream.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.basefream.entity.CmaPcArea;
import com.example.basefream.service.CmaPcAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @author L-HN
* @since 2022-04-27
*/
@Slf4j
@Api(tags = {"行政区划表"})
@RestController
@RequestMapping("/cmaPcArea")
public class CmaPcAreaController {
    
    @Autowired
    public CmaPcAreaService cmaPcAreaService;

    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public Object save(@RequestBody CmaPcArea cmaPcArea){
        boolean b = cmaPcAreaService.save(cmaPcArea);
        return b;
    }

    @ApiOperation(value = "根据id删除")
    @PostMapping("/delete/{id}")
    public Object delete(@PathVariable("id") Long id){
        boolean b = cmaPcAreaService.removeById(id);
        return b;
    }

    @ApiOperation(value = "批量删除")
    @PostMapping("/delete/batch")
    public Object deleteBatch(@RequestBody String ids){
        List<Long> idList = (List<Long>)JSON.parse(ids);
        boolean b = cmaPcAreaService.removeByIds(idList);
        return b;
    }

    @ApiOperation(value = "列表（分页）")
    @GetMapping("/list")
    public Object list(Long pageNum, Long pageSize){
        IPage<CmaPcArea> page = cmaPcAreaService.page(
                new Page<>(pageNum, pageSize),
                new LambdaQueryWrapper<CmaPcArea>().eq(false, CmaPcArea::getAreaId, 0));
        return page;
    }

    @ApiOperation(value = "详情")
    @GetMapping("/get/{id}")
    public Object get(@PathVariable("id") Long id){
        CmaPcArea cmaPcArea = cmaPcAreaService.getById(id);
        return cmaPcArea;
    }

    @ApiOperation(value = "根据id修改")
    @PostMapping("/update/{id}")
    public Object update(@PathVariable("id") String id, @RequestBody CmaPcArea cmaPcArea){
        cmaPcArea.setAreaId(id);
        boolean b = cmaPcAreaService.updateById(cmaPcArea);
        return b;
    }

    @ApiOperation(value = "批量修改")
    @PostMapping("/update/batch")
    public Object updateBatch(@RequestBody String cmaPcAreas){
        List<CmaPcArea> cmaPcAreaList = (List<CmaPcArea>)JSON.parse(cmaPcAreas);
        boolean b = cmaPcAreaService.updateBatchById(cmaPcAreaList);
        return b;
    }

}